using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace xboxssbeatemup
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class HUD : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public SpriteFont font1 = null;
        public Color color = Color.White;
        private int _topY = 10;
        private int _bottomY = 100;
        private int _leftX = 10;
        private int _rightX = 180;
        public Stats[] ps;
        public int numPlayers;
        //public SpriteBatch sb;

        public HUD(Game game, Player[] players)
            : base(game)
        {
            numPlayers = players.Length;
            ps = new Stats[players.Length];
            for (int i = 0; i < players.Length; i++)
            {
                ps[i] = players[i].stats;
            }
        }

        protected override void LoadContent()
        {
            font1 = this.Game.Content.Load<SpriteFont>("Fonts\\Arial");
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (!((Game1)this.Game).editor.isEditing)
            {
                SpriteBatch sb = ((Game1)this.Game).SpriteBatch;

                sb.Begin();

                if (numPlayers >= 4)
                {
                    sb.DrawString(font1, "Player 4 Stats:", new Vector2(this.Game.Window.ClientBounds.Width - _rightX, this.Game.Window.ClientBounds.Height - _bottomY), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Health: " + ps[3].health.ToString() + "/" + ps[3].healthMax.ToString(), new Vector2(this.Game.Window.ClientBounds.Width - _rightX, this.Game.Window.ClientBounds.Height - _bottomY + 20), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Magic: " + ps[3].magic.ToString() + "/" + ps[3].magicMax.ToString(), new Vector2(this.Game.Window.ClientBounds.Width - _rightX, this.Game.Window.ClientBounds.Height - _bottomY + 35), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Experience: " + ps[3].experience.ToString(), new Vector2(this.Game.Window.ClientBounds.Width - _rightX, this.Game.Window.ClientBounds.Height - _bottomY + 50), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Gold: " + ps[3].gold.ToString(), new Vector2(this.Game.Window.ClientBounds.Width - _rightX, this.Game.Window.ClientBounds.Height - _bottomY + 65), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                }
                if (numPlayers >= 3)
                {
                    sb.DrawString(font1, "Player 3 Stats", new Vector2(_leftX, this.Game.Window.ClientBounds.Height - _bottomY), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Health: " + ps[2].health.ToString() + "/" + ps[2].healthMax.ToString(), new Vector2(_leftX, this.Game.Window.ClientBounds.Height - _bottomY + 20), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Magic: " + ps[2].magic.ToString() + "/" + ps[2].magicMax.ToString(), new Vector2(_leftX, this.Game.Window.ClientBounds.Height - _bottomY + 35), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Experience: " + ps[2].experience.ToString(), new Vector2(_leftX, this.Game.Window.ClientBounds.Height - _bottomY + 50), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Gold: " + ps[2].gold.ToString(), new Vector2(_leftX, this.Game.Window.ClientBounds.Height - _bottomY + 65), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                }
                if (numPlayers >= 2)
                {
                    sb.DrawString(font1, "Player 2 Stats", new Vector2(this.Game.Window.ClientBounds.Width - _rightX, _topY), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Health: " + ps[1].health.ToString() + "/" + ps[1].healthMax.ToString(), new Vector2(this.Game.Window.ClientBounds.Width - _rightX, _topY + 20), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Magic: " + ps[1].magic.ToString() + "/" + ps[1].magicMax.ToString(), new Vector2(this.Game.Window.ClientBounds.Width - _rightX, _topY + 35), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Experience: " + ps[1].experience.ToString(), new Vector2(this.Game.Window.ClientBounds.Width - _rightX, _topY + 50), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Gold: " + ps[1].gold.ToString(), new Vector2(this.Game.Window.ClientBounds.Width - _rightX, _topY + 65), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                }
                if (numPlayers >= 1)
                {
                    sb.DrawString(font1, "Player 1 Stats", new Vector2(_leftX, _topY), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Health: " + ps[0].health.ToString() + "/" + ps[0].healthMax.ToString(), new Vector2(_leftX, _topY + 20), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Magic: " + ps[0].magic.ToString() + "/" + ps[0].magicMax.ToString(), new Vector2(_leftX, _topY + 35), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Experience: " + ps[0].experience.ToString(), new Vector2(_leftX, _topY + 50), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                    sb.DrawString(font1, "Gold: " + ps[0].gold.ToString(), new Vector2(_leftX, _topY + 65), color, 0, Vector2.Zero, 1, SpriteEffects.None, 0.100f);
                }

                sb.End();
            }
                // Fix RenderStates for 3d that get messed up by SpriteBatch
                GraphicsDevice.RenderState.DepthBufferEnable = true;
                GraphicsDevice.RenderState.AlphaBlendEnable = false;
                GraphicsDevice.RenderState.AlphaTestEnable = false;
                GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
                GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
        }

    }
}